/*
 * list.c
 *
 *  Created on: 2011-9-26
 *      Author: luxiao
 */

#include <list.h>

list *listCreate(void) {
	struct list *list;

	if(list = malloc(sizeof(*list)) == NULL)
		return NULL;
	list->head = list->tail = NULL;
	list->len = 0;
	return list;
}

void listRelease(list * list)
{
	int len;
	listNode *current, *next;

	current = list->head;
	len = list->len;
	while(len --) {
		next = current->next;
		free(current->next);
		current = next;
	}
	free(list);
}

list *listAddNodeHead(list *list, void *value)
{
	listNode *node;

	if((node = malloc(sizeof(*node))) == NULL)
		return NULL;
	node->value = value;
	    if (list->len == 0) {
	        list->head = list->tail = node;
	        node->pre = node->next = NULL;
	    } else {
	        node->pre = NULL;
	        node->next = list->head;
	        list->head->pre = node;
	        list->head = node;
	    }
	    list->len++;
	    return list;
}

list *listAddNodeTail(list *list, void *value)
{
    listNode *node;

    if ((node = malloc(sizeof(*node))) == NULL)
        return NULL;
    node->value = value;
    if (list->len == 0) {
        list->head = list->tail = node;
        node->pre = node->next = NULL;
    } else {
        node->pre = list->tail;
        node->next = NULL;
        list->tail->next = node;
        list->tail = node;
    }
    list->len++;
    return list;
}
